package org.apache.james.mime4j.message.storage;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleTempStorage extends TempStorage
{
  static Class class$org$apache$james$mime4j$message$storage$SimpleTempStorage;
  private static Log log;
  private Random random;
  private TempPath rootPath = null;

  static
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$storage$SimpleTempStorage == null)
    {
      localClass = class$("org.apache.james.mime4j.message.storage.SimpleTempStorage");
      class$org$apache$james$mime4j$message$storage$SimpleTempStorage = localClass;
    }
    while (true)
    {
      log = LogFactory.getLog(localClass);
      return;
      localClass = class$org$apache$james$mime4j$message$storage$SimpleTempStorage;
    }
  }

  public SimpleTempStorage()
  {
    this.random = new Random();
    this.rootPath = new SimpleTempPath(this, System.getProperty("java.io.tmpdir"), null);
  }

  static TempFile access$300(SimpleTempStorage paramSimpleTempStorage, TempPath paramTempPath, String paramString1, String paramString2)
    throws IOException
  {
    return paramSimpleTempStorage.createTempFile(paramTempPath, paramString1, paramString2);
  }

  static TempPath access$400(SimpleTempStorage paramSimpleTempStorage, TempPath paramTempPath, String paramString)
    throws IOException
  {
    return paramSimpleTempStorage.createTempPath(paramTempPath, paramString);
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  // ERROR //
  private TempFile createTempFile(TempPath paramTempPath, String paramString1, String paramString2)
    throws IOException
  {
    // Byte code:
    //   0: aload_2
    //   1: ifnonnull +6 -> 7
    //   4: ldc 89
    //   6: astore_2
    //   7: aload_3
    //   8: ifnonnull +6 -> 14
    //   11: ldc 91
    //   13: astore_3
    //   14: aconst_null
    //   15: astore 4
    //   17: sipush 1000
    //   20: istore 5
    //   22: aload_0
    //   23: monitorenter
    //   24: aload 4
    //   26: pop
    //   27: aload_0
    //   28: getfield 48	org/apache/james/mime4j/message/storage/SimpleTempStorage:random	Ljava/util/Random;
    //   31: invokevirtual 95	java/util/Random:nextLong	()J
    //   34: invokestatic 101	java/lang/Math:abs	(J)J
    //   37: lstore 8
    //   39: new 103	java/io/File
    //   42: dup
    //   43: aload_1
    //   44: invokeinterface 109 1 0
    //   49: new 111	java/lang/StringBuffer
    //   52: dup
    //   53: invokespecial 112	java/lang/StringBuffer:<init>	()V
    //   56: aload_2
    //   57: invokevirtual 116	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   60: lload 8
    //   62: invokevirtual 119	java/lang/StringBuffer:append	(J)Ljava/lang/StringBuffer;
    //   65: aload_3
    //   66: invokevirtual 116	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   69: invokevirtual 122	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   72: invokespecial 125	java/io/File:<init>	(Ljava/lang/String;Ljava/lang/String;)V
    //   75: astore 4
    //   77: iinc 5 255
    //   80: aload 4
    //   82: invokevirtual 129	java/io/File:exists	()Z
    //   85: ifeq +8 -> 93
    //   88: iload 5
    //   90: ifgt -66 -> 24
    //   93: aload 4
    //   95: invokevirtual 129	java/io/File:exists	()Z
    //   98: ifeq +18 -> 116
    //   101: new 63	IOException
    //   104: dup
    //   105: ldc 131
    //   107: invokespecial 134	IOException:<init>	(Ljava/lang/String;)V
    //   110: athrow
    //   111: aload_0
    //   112: monitorexit
    //   113: aload 7
    //   115: athrow
    //   116: aload 4
    //   118: invokevirtual 137	java/io/File:createNewFile	()Z
    //   121: pop
    //   122: aload_0
    //   123: monitorexit
    //   124: new 8	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile
    //   127: dup
    //   128: aload 4
    //   130: aconst_null
    //   131: invokespecial 140	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:<init>	(Ljava/io/File;Lorg/apache/james/mime4j/message/storage/SimpleTempStorage$1;)V
    //   134: areturn
    //   135: astore 10
    //   137: new 63	IOException
    //   140: dup
    //   141: new 111	java/lang/StringBuffer
    //   144: dup
    //   145: invokespecial 112	java/lang/StringBuffer:<init>	()V
    //   148: ldc 142
    //   150: invokevirtual 116	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   153: aload 4
    //   155: invokevirtual 143	java/io/File:getAbsolutePath	()Ljava/lang/String;
    //   158: invokevirtual 116	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   161: ldc 145
    //   163: invokevirtual 116	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   166: invokevirtual 122	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   169: invokespecial 134	IOException:<init>	(Ljava/lang/String;)V
    //   172: athrow
    //   173: astore 7
    //   175: goto -64 -> 111
    //   178: astore 7
    //   180: goto -69 -> 111
    //
    // Exception table:
    //   from	to	target	type
    //   116	122	135	IOException
    //   27	77	173	finally
    //   80	113	178	finally
    //   116	122	178	finally
    //   122	124	178	finally
    //   137	173	178	finally
  }

  private TempPath createTempPath(TempPath paramTempPath, String paramString)
    throws IOException
  {
    File localFile;
    if (paramString == null)
      paramString = "";
    int i = 1000;
    do
    {
      long l = Math.abs(this.random.nextLong());
      localFile = new File(paramTempPath.getAbsolutePath(), paramString + l);
      --i;
    }
    while ((localFile.exists()) && (i > 0));
    if ((localFile.exists()) || (!(localFile.mkdirs())))
    {
      log.error("Unable to mkdirs on " + localFile.getAbsolutePath());
      throw new IOException("Creating dir '" + localFile.getAbsolutePath() + "' failed.");
    }
    return new SimpleTempPath(this, localFile, null);
  }

  public TempPath getRootTempPath()
  {
    return this.rootPath;
  }

  private static class SimpleTempFile
  implements TempFile
  {
    private static final Set filesToDelete = new HashSet();
    private File file;

    private SimpleTempFile(File paramFile)
    {
      this.file = null;
      this.file = paramFile;
      this.file.deleteOnExit();
    }

    SimpleTempFile(File paramFile, SimpleTempStorage.1 param1)
    {
      this(paramFile);
    }

    // ERROR //
    public void delete()
    {
      // Byte code:
      //   0: getstatic 22	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:filesToDelete	Ljava/util/Set;
      //   3: astore_1
      //   4: aload_1
      //   5: monitorenter
      //   6: aload_0
      //   7: getfield 26	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:file	Ljava/io/File;
      //   10: ifnull +21 -> 31
      //   13: getstatic 22	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:filesToDelete	Ljava/util/Set;
      //   16: aload_0
      //   17: getfield 26	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:file	Ljava/io/File;
      //   20: invokeinterface 41 2 0
      //   25: pop
      //   26: aload_0
      //   27: aconst_null
      //   28: putfield 26	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:file	Ljava/io/File;
      //   31: getstatic 22	org/apache/james/mime4j/message/storage/SimpleTempStorage$SimpleTempFile:filesToDelete	Ljava/util/Set;
      //   34: invokeinterface 45 1 0
      //   39: astore_3
      //   40: aload_3
      //   41: invokeinterface 51 1 0
      //   46: ifeq +32 -> 78
      //   49: aload_3
      //   50: invokeinterface 55 1 0
      //   55: checkcast 28	java/io/File
      //   58: invokevirtual 57	java/io/File:delete	()Z
      //   61: ifeq -21 -> 40
      //   64: aload_3
      //   65: invokeinterface 60 1 0
      //   70: goto -30 -> 40
      //   73: astore_2
      //   74: aload_1
      //   75: monitorexit
      //   76: aload_2
      //   77: athrow
      //   78: aload_1
      //   79: monitorexit
      //   80: return
      //
      // Exception table:
      //   from	to	target	type
      //   6	76	73	finally
      //   78	80	73	finally
    }

    public String getAbsolutePath()
    {
      return this.file.getAbsolutePath();
    }

    public InputStream getInputStream()
      throws IOException
    {
      return new BufferedInputStream(new FileInputStream(this.file));
    }

    public OutputStream getOutputStream()
      throws IOException
    {
      return new BufferedOutputStream(new FileOutputStream(this.file));
    }

    public boolean isInMemory()
    {
      return false;
    }

    public long length()
    {
      return this.file.length();
    }
  }

  private class SimpleTempPath
  implements TempPath
  {
    private File path;
    private final SimpleTempStorage this$0;

    private SimpleTempPath(, File paramFile)
    {
      this.path = null;
      this.path = paramFile;
    }

    SimpleTempPath(, File paramFile, SimpleTempStorage.1 param1)
    {
      this(paramSimpleTempStorage, paramFile);
    }

    private SimpleTempPath(, String paramString)
    {
      this.path = null;
      this.path = new File(paramString);
    }

    SimpleTempPath(, String paramString, SimpleTempStorage.1 param1)
    {
      this(paramSimpleTempStorage, paramString);
    }

    public TempFile createTempFile()
      throws IOException
    {
      return SimpleTempStorage.access$300(this.this$0, this, null, null);
    }

    public TempFile createTempFile(, String paramString2)
      throws IOException
    {
      return SimpleTempStorage.access$300(this.this$0, this, paramString1, paramString2);
    }

    public TempFile createTempFile(, String paramString2, boolean paramBoolean)
      throws IOException
    {
      return SimpleTempStorage.access$300(this.this$0, this, paramString1, paramString2);
    }

    public TempPath createTempPath()
      throws IOException
    {
      return SimpleTempStorage.access$400(this.this$0, this, null);
    }

    public TempPath createTempPath()
      throws IOException
    {
      return SimpleTempStorage.access$400(this.this$0, this, paramString);
    }

    public void delete()
    {
    }

    public String getAbsolutePath()
    {
      return this.path.getAbsolutePath();
    }
  }
}